package com.kangye.exam.controller;

import com.github.pagehelper.PageInfo;
import com.kangye.exam.bean.Student;
import com.kangye.exam.service.StudentService;
import com.kangye.exam.utils.JsonUtil;
import com.kangye.exam.utils.RedisTool;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/student")
@CrossOrigin
public class StudentController {

    @Resource
    private StudentService studentService;
    @Resource
    private RedisTool redisTool;


    //学生登录接口
    @RequestMapping("/login")
    public Object studentLogin(String studentUser, String studentPass, HttpServletRequest req){
        //map集合用于前台传值
        Map<String,Object> map = new HashMap<>();
        if(studentUser.equals(null)||studentPass.equals("")||studentPass.equals(null)||studentPass.equals("")){
            map.put("code",-2);
            return map;
        }
        //根据sessionid判断redis中是否有此缓存
        if(redisTool.hget("stu",req.getSession().getId())!=null){
            //如果有就查出来该对象比较用户名和密码
            Student stu = JsonUtil.json2Bean(redisTool.hget("stu",req.getSession().getId()).toString(),Student.class);
            if(studentUser.equals(stu.getStudentUser())&&studentPass.equals(stu.getStudentPass())){
                //用户名和密码匹配成功返回前台数据
                map.put("code", 0);
                return map;
            }
        }
        //没有用户登陆从数据库中查询用户
        Student u =  studentService.studentLogin(studentUser,studentPass);
        //数据库是否存在该用户
        if(u != null){
            //将当前用户转换为json格式存到map中
            String student = JsonUtil.bean2Json(u);
            Map<String,Object> stuMap = new HashMap<>();
            stuMap.put(req.getSession().getId(),student);
            //把当前用户存放到redis中,设置用户过期时间3600s
            redisTool.hmset("stu",stuMap,3600);
            map.put("code",0);
            map.put("stuId",u.getStudentId());
        }else{
            //数据库中没有该用户,登陆失败,map中,传给前台登陆失败代码 code:-1
            map.put("code",-1);
        }
        return map;
    }


    //学生修改信息时返显数据接口
    @RequestMapping("/findStuById")
    public Object findStuById(Integer studentId,HttpServletRequest req){
        Map<String,Object> map = new HashMap<>();
        if(studentId.equals(null)||studentId.equals("")){
            map.put("code",-1);
            return map;
        }
        //根据sessionid判断redis中是否有此缓存
        if(redisTool.hget("stu",req.getSession().getId())!=null){
            //如果有再对比学生id是否一致
            Student stu = JsonUtil.json2Bean(redisTool.hget("stu",req.getSession().getId()).toString(),Student.class);
            if(studentId.equals(stu.getStudentId())){
                //如果学生id一致,代表为同一个学生,直接返回

                map.put("code",0);
                map.put("student",stu);
                return map;
            }
        }
        return studentService.findStuById(studentId,req.getSession().getId());
    }

    //学生修改个人信息接口
    @RequestMapping("/update")
    public Object studentUpd(Student student, HttpServletRequest req){
        Map<String,Object> map = new HashMap<>();
        if(student.getStudentAge()>40||student.getStudentAge()<10){
            map.put("code",-1);
            map.put("massage","年龄应在10~40岁之间!");
            return map;
        }else if(student.getStudentTel().length()!=11){
            map.put("code",-1);
            map.put("massage","手机号不符合规范!");
            return map;
        }
        if(studentService.stuUpd(student)){
            //修改完成后应该再查一遍数据库再把这个用户放到redis中
            map = studentService.findStuById(student.getStudentId(),req.getSession().getId());
        }else{
            map.put("code",-1);
        }
        return map;
    }


    /**
     * 查询所有数据
     *
     * @param pageNum   当前页
     * @param pageSize  每页条数
     * @return 对象列表
     */
    @RequestMapping("/stuAll")
    public Object querySAll(Integer pageNum, Integer pageSize) {
        PageInfo<Student> info = studentService.querySAll(pageNum,pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("count", info.getTotal());
        map.put("limit", info.getPageSize());
        map.put("data", info.getList());
        return map;
    }

    /**
     * 条件查询->通过关键字(用户或姓名)查询数据
     *
     * @param pageNum   当前页
     * @param pageSize  每页条数
     * @param name      关键字
     * @return 对象列表
     */
    @RequestMapping("/quStuList")
    public Object studentList(String name, Integer pageNum, Integer pageSize) {
        PageInfo<Student> info = studentService.studentList(name,pageNum,pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("count", info.getTotal());
        map.put("limit", info.getPageSize());
        map.put("data", info.getList());
        return map;
    }

    /**
     * 通过教师id筛选查询
     *
     * @param teacherId 老师id
     * @param pageSize 每页条数
     * @param pageNum  当前页
     * @return 对象列表
     */
    @RequestMapping("/ftStu")
    public Object findTSById(Integer teacherId, Integer pageNum, Integer pageSize) {
        PageInfo<Student> stu = studentService.findTId(teacherId, pageNum, pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("info",stu);
        return map;
    }

    /**
     * 新增数据
     *
     * @param student 实例对象
     */
    @RequestMapping("/inStu")
    public Object insertStu(Student student) {
        Date date = new Date();//创建日期对象
        //获取当前时间  格式:2021-01-20 13:33:07
        String sd = String.format("%tF", date) + " " + String.format("%tT", date);
        student.setStudentCreate(sd);
        studentService.insertStu(student);
        return null;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @RequestMapping("/fiStu")
    public Object findSById(int id) {
        Map<String,Object> map = new HashMap<>();
        Student s = studentService.findSId(id);
        map.put("data",s);
        return map;
    }

    /**
     * 修改数据
     *
     * @param student 实例对象
     */
    @RequestMapping("/upStu")
    public Object updateStu(Student student) {
        studentService.updateStu(student);
        Map<String,Object> map = new HashMap<>();
        map.put("code", 0);
        return map;
    }

    /**
     * 通过主键删除数据
     *
     * @param studentId 实例对象
     */
    @RequestMapping("/deStu")
    public Object deleteSId(int studentId) {
        studentService.deleteSId(studentId);
        Map<String,Object> map = new HashMap<>();
        map.put("code", 0);
        return map;
    }

}
